package com.project.datacenter.controller;


import com.project.datacenter.dto.BusLineStationDto;
import com.project.datacenter.dto.ParamDto;
import com.project.datacenter.dto.UserXlDto;
import com.project.datacenter.entity.*;
import com.project.datacenter.service.*;
import com.project.datacenter.util.RestResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/datacenter/")
public class DataCenterController {

    @Resource
    BusLineService busLineService;
    @Resource
    BusLineStationService busLineStationService;
    @Resource
    BusVehicleService busVehicleService;
    @Resource
    BusDirectionService busDirectionService;
    @Resource
    EnterpriseService enterpriseService;
    @Resource
    UserXlService userXlService;

    @PostMapping("busline")
    public RestResult busline(@RequestBody ParamDto paramDto) {
        List<TobjBusLine> tobjBusLineList = new ArrayList<>();
        if (paramDto.getType() == 1) {
            tobjBusLineList = busLineService.getListByDate(paramDto);
        } else {
            tobjBusLineList = busLineService.getList();
        }
        if (tobjBusLineList.size() > 0) {
            busLineService.deleteByDate(paramDto);
            for (TobjBusLine tobjBusLine : tobjBusLineList) {
                tobjBusLine.setRq(paramDto.getDate());
            }
            busLineService.saveList(tobjBusLineList);
        }
        // 线路走向
        List<TobjBusDirection> tobjBusDirectionList = new ArrayList<>();
        if (paramDto.getType() == 1) {
            tobjBusDirectionList = busDirectionService.getListByDate(paramDto);
        } else {
            tobjBusDirectionList = busDirectionService.getList();
        }
        if (tobjBusDirectionList.size() > 0) {
            busDirectionService.deleteByDate(paramDto);
            for (TobjBusDirection tobjBusDirection : tobjBusDirectionList) {
                tobjBusDirection.setRq(paramDto.getDate());
            }
            busDirectionService.saveList(tobjBusDirectionList);
        }
        // 配车数
        List<TobjBusVehicle> tobjBusVehicleList = new ArrayList<>();
        if (paramDto.getType() == 1) {
            tobjBusVehicleList = busVehicleService.getListByDate(paramDto);
        } else {
            tobjBusVehicleList = busVehicleService.getList();
        }
        if (tobjBusVehicleList.size() > 0) {
            busVehicleService.deleteByDate(paramDto);
            for (TobjBusVehicle tobjBusVehicle : tobjBusVehicleList) {
                tobjBusVehicle.setRq(paramDto.getDate());
            }
            busVehicleService.saveList(tobjBusVehicleList);
        }
        List<TobjBusLine> list = tobjBusLineList.stream().filter(item -> item.getIsremove() != null && item.getIsremove() == 0).collect(Collectors.toList());
        if (list.size() > 0) {
            List<TobjBusDirection> directionList = new ArrayList<>();
            if (tobjBusDirectionList.size() > 0) {
                directionList = tobjBusDirectionList.stream().filter(item -> item.getIsremove() != null && item.getIsremove() == 0).collect(Collectors.toList());
            }
            List<TobjBusVehicle> vehicleList = new ArrayList<>();
            if (tobjBusVehicleList.size() > 0) {
                vehicleList = tobjBusVehicleList.stream().filter(item -> item.getIsremove() != null && item.getIsremove() == 0).collect(Collectors.toList());
            }
            List<BusLine> newBusLineList = busLineService.update(list, directionList, vehicleList);
            if (newBusLineList.size() > 0) {
                for (BusLine newBusLine : newBusLineList) {
                    if (newBusLine.getState() == 0) {
                        // 管理员账号线路增加
                        UserXl userXl = new UserXl();
                        userXl.setJgxlid(newBusLine.getJgxlid());
                        userXl.setUid("admin");
                        userXlService.save(userXl);
                        UserXl userXl2 = new UserXl();
                        userXl2.setJgxlid(newBusLine.getJgxlid());
                        userXl2.setUid("adminsoft");
                        userXlService.save(userXl2);
                        // 线路所属账号增加
                        List<UserXlDto> userXlDtos = userXlService.getUserYhxxList(newBusLine);
                        for (UserXlDto userXlDto : userXlDtos) {
                            if (!("admin".equals(userXlDto.getUid()) || "adminsoft".equals(userXlDto.getUid()))) {
                                UserXl userXl3 = new UserXl();
                                userXl3.setJgxlid(newBusLine.getJgxlid());
                                userXl3.setUid(userXlDto.getUid());
                                userXlService.save(userXl3);
                            }
                        }
                    } else {
                        userXlService.delete(newBusLine.getJgxlid());
                    }
                }
            }
            busLineService.updateZjLine();
        }
        return RestResult.ok();
    }

    @PostMapping("busline/station")
    public RestResult buslineStation(@RequestBody ParamDto paramDto) {
        List<TobjBusLineStation> tobjBusLineStationList = new ArrayList<>();
        if (paramDto.getType() == 1) {
            tobjBusLineStationList = busLineStationService.getListByDate(paramDto);
        } else {
            tobjBusLineStationList = busLineStationService.getList();
        }
        if (tobjBusLineStationList.size() > 0) {
            busLineStationService.deleteByDate(paramDto);
            for (TobjBusLineStation tobjBusLineStation : tobjBusLineStationList) {
                tobjBusLineStation.setRq(paramDto.getDate());
            }
            busLineStationService.saveList(tobjBusLineStationList);
        }
        List<TobjBusLineStation> list = tobjBusLineStationList.stream().filter(item -> item.getIsremove() != null && item.getIsremove() == 0).collect(Collectors.toList());
        if (list.size() > 0) {
            busLineStationService.deleteAll();
            busLineStationService.update(list);
        }
        // 重复项删除
        List<BusLineStationDto> repeatList = busLineStationService.getRepeatAll();
        if (repeatList.size() > 0) {
            for (BusLineStationDto busLineStationDto : repeatList) {
                List<BusLineStation> deleteList = busLineStationService.getByRepeat(busLineStationDto);
                if (deleteList.size() > 0) {
                    for (int i = 1; i < deleteList.size(); i++) {
                        busLineStationService.deleteById(deleteList.get(i));
                    }
                }
            }
        }
        // 更新特殊高峰线路时间
        List<TobjBusLineStation> firstList = busLineStationService.getFirst();
        List<TobjBusLineStation> maxList = busLineStationService.getMax();
        if (firstList.size() > 0) {
            for (TobjBusLineStation tobjBusLineStation : firstList) {
                if (maxList.size() > 0) {
                    List<TobjBusLineStation> newList = maxList.stream().filter(item -> item.getBusline_id().equals(tobjBusLineStation.getBusline_id())).collect(Collectors.toList());
                    if (newList.size() > 0) {
                        if (!StringUtils.isEmpty(newList.get(0).getPeakendtime())) {
                            BusLineStation busLineStation1 = new BusLineStation();
                            busLineStation1.setId(tobjBusLineStation.getBusstop_id());
                            busLineStation1.setXlid(tobjBusLineStation.getBusline_id());
                            busLineStation1.setMbsj(newList.get(0).getPeakendtime());
                            busLineStationService.updateSzmbById(busLineStation1);
                        }
                    }
                }
            }
        }
        if (maxList.size() > 0) {
            for (TobjBusLineStation tobjBusLineStation : maxList) {
                BusLineStation busLineStation00 = new BusLineStation();
                busLineStation00.setId(tobjBusLineStation.getBusstop_id());
                busLineStation00.setXlid(tobjBusLineStation.getBusline_id());
                busLineStation00.setSbsj(tobjBusLineStation.getPeakendtime());
                busLineStation00.setZj(String.valueOf(tobjBusLineStation.getStationlevel()));
                busLineStationService.updateMzsbById(busLineStation00);
            }
        }
        // 更新首末站名称
        busLineStationService.updateYgmc();
        return RestResult.ok();
    }

    @PostMapping("enterprise")
    public RestResult enterprise(@RequestBody ParamDto paramDto) {
        List<TobjEnterprise> tobjEnterpriseList = new ArrayList<>();
        if (paramDto.getType() == 1) {
            tobjEnterpriseList = enterpriseService.getListByDate(paramDto);
        } else {
            tobjEnterpriseList = enterpriseService.getList();
        }
        if (tobjEnterpriseList.size() > 0) {
            enterpriseService.deleteByDate(paramDto);
            for (TobjEnterprise tobjEnterprise : tobjEnterpriseList) {
                tobjEnterprise.setRq(paramDto.getDate());
            }
            enterpriseService.saveList(tobjEnterpriseList);
            List<TobjEnterprise> list = tobjEnterpriseList.stream().filter(item -> item.getIsremove() != null && item.getIsremove() == 0).collect(Collectors.toList());
            if (list.size() > 0) {
                enterpriseService.deleteAll();
                enterpriseService.insert(list);
            }
        }
        return RestResult.ok();
    }
}
